/**
 * 家族相关 API
 */

import request from '../request'

/**
 * 家族信息接口
 */
export interface FamilyInfo {
  id: string
  name: string
  description?: string
  patriarch: string
  members: number
  createdAt: string
  updatedAt?: string
  contractAddress?: string
}

/**
 * 家族成员接口
 */
export interface FamilyMember {
  address: string
  role: string
  name?: string
  joinedAt: string
}

/**
 * 注册家族请求参数
 */
export interface RegisterFamilyRequest {
  name: string
  description: string
}

/**
 * 添加成员请求参数
 */
export interface AddMemberRequest {
  familyId: string
  memberAddress: string
  role: string
  name?: string
}

/**
 * 家族 API
 */
export const familyApi = {
  /**
   * 注册家族
   */
  registerFamily: (data: RegisterFamilyRequest) =>
    request.post<{ familyId: string; txHash: string }>('/blockchain/family/register', data),

  /**
   * 获取家族信息
   */
  getFamilyInfo: (familyId: string) =>
    request.get<FamilyInfo>(`/blockchain/family/${familyId}`),

  /**
   * 添加成员
   */
  addMember: (data: AddMemberRequest) =>
    request.post<{ txHash: string }>('/blockchain/family/member', data),

  /**
   * 移除成员
   */
  removeMember: (familyId: string, memberAddress: string) =>
    request.delete<{ txHash: string }>(`/blockchain/family/${familyId}/member/${memberAddress}`),

  /**
   * 获取家族成员列表
   */
  getMembers: (familyId: string) =>
    request.get<FamilyMember[]>(`/blockchain/family/${familyId}/members`),

  /**
   * 获取家族列表
   */
  getFamilyList: () =>
    request.get<FamilyInfo[]>('/blockchain/family/list'),

  /**
   * 获取用户的家族
   */
  getUserFamilies: (address: string) =>
    request.get<FamilyInfo[]>(`/blockchain/family/user/${address}`),

  /**
   * 更新家族信息
   */
  updateFamily: (familyId: string, data: Partial<FamilyInfo>) =>
    request.put<{ txHash: string }>(`/blockchain/family/${familyId}`, data),

  /**
   * 转让族长权限
   */
  transferPatriarch: (familyId: string, newPatriarch: string) =>
    request.post<{ txHash: string }>(`/blockchain/family/${familyId}/transfer-patriarch`, {
      newPatriarch,
    }),
}

export default familyApi

